﻿    
    
    
    setInterval(myFunc, 1000, 'demo');


    function myFunc(arg) {
        var fs = require('fs');

        var invoke_count=0;
        fs.exists('./data.txt', function (exists) {
            if (!exists) {
                fs.writeFile('./data.txt', invoke_count);
            } else {
                
                fs.readFile("./data.txt", function (error, data) {

                    if (error) {
                        console.log('读取文件失败')
                    }
                    else {
                        
                        invoke_count = (data.toString() == "" ? "0" : parseInt(data.toString())); 
                        
                        var http = require('http');

                        var request_timer = null, req = null;
                        // 请求5秒超时
                        request_timer = setTimeout(function () {
                            req.abort();
                            console.log('请求已超时');
                        }, 5000);

                        var options = {
                            host: 'http://localhost:',
                            port: 52417,
                            path: 'GetDeviceInfo.aspx'
                        };
                        req = http.get(options, function (res) {
                            clearTimeout(request_timer);

                            // 等待响应60秒超时
                            var response_timer = setTimeout(function () {
                                res.destroy();
                                console.log('请求已超时.');
                            }, 60000);

                            var chunks = [], length = 0;
                            res.on('data', function (chunk) {
                                length += chunk.length;
                                chunks.push(chunk);
                            });
                            res.on('end', function () {
                                clearTimeout(response_timer);
                                var data = new Buffer(length);
                                // 延后copy
                                for (var i = 0, pos = 0, size = chunks.length; i < size; i++) {
                                    chunks[i].copy(data, pos);
                                    pos += chunks[i].length;
                                }
                                console.log("调用接口次数:" + invoke_count);
                                invoke_count += 1;

                                fs.writeFile('./data.txt', invoke_count, function (error) {
                                    if (error) {
                                        console.log('写入错误')
                                    } else {
                                    }

                                });
                            });
                        }).on('error', function (e) {
                            // 响应头有错误
                            clearTimeout(request_timer);
                            console.log("返回错误：" + e.message);
                        });
                    }
                });
            }
        }); 
        
        
    }